package com.alin.swing.component.base;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 * @ClassName J6JTextFieldMain
 * @Description: 文本框:JTextField 用来编辑单行的文本。
 * @Author chenlin
 * @Date 2021/6/7 15:42
 * @Version V1.0
 **/
public class J6JTextFieldMain {
    public J6JTextFieldMain(){
        JFrame jf = new JFrame("测试窗口");
        jf.setSize(300, 300);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();

        // 创建文本框，指定可见列数为8列
        final JTextField textField = new JTextField(8);
        textField.setFont(new Font(null, Font.PLAIN, 20));
        //设置单行文本的内容
        textField.setText("默认文本~");
        panel.add(textField);
        // 创建一个按钮，点击后获取文本框中的文本
        JButton btn = new JButton("提交");
        btn.setFont(new Font(null, Font.PLAIN, 20));
        //添加时间监听
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("提交: " + textField.getText());
            }
        });
        panel.add(btn);

        jf.setContentPane(panel);
        jf.setVisible(true);
    }

    public static void main(String[] args) {
        new J6JTextFieldMain();
    }
}
